package cn.iocoder.yudao.hires.service.impl;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.hires.service.DictSpecializedService;
import cn.iocoder.yudao.hires.vo.DictSpecializedPageReqVO;
import cn.iocoder.yudao.hires.vo.DictSpecializedSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.hires.entity.DictSpecializedDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.hires.mapper.DictSpecializedMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * 专业字典 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DictSpecializedServiceImpl implements DictSpecializedService {

    @Resource
    private DictSpecializedMapper dictSpecializedMapper;

    @Override
    public Integer createDictSpecialized(DictSpecializedSaveReqVO createReqVO) {
        // 插入
        DictSpecializedDO dictSpecialized = BeanUtils.toBean(createReqVO, DictSpecializedDO.class);
        dictSpecializedMapper.insert(dictSpecialized);
        // 返回
        return dictSpecialized.getId();
    }

    @Override
    public void updateDictSpecialized(DictSpecializedSaveReqVO updateReqVO) {
        // 校验存在
        validateDictSpecializedExists(updateReqVO.getId());
        // 更新
        DictSpecializedDO updateObj = BeanUtils.toBean(updateReqVO, DictSpecializedDO.class);
        dictSpecializedMapper.updateById(updateObj);
    }

    @Override
    public void deleteDictSpecialized(Integer id) {
        // 校验存在
        validateDictSpecializedExists(id);
        // 删除
        dictSpecializedMapper.deleteById(id);
    }

    private void validateDictSpecializedExists(Integer id) {
        if (dictSpecializedMapper.selectById(id) == null) {
            throw exception(new ErrorCode(500, "专业字典不存在"));
        }
    }

    @Override
    public DictSpecializedDO getDictSpecialized(Integer id) {
        return dictSpecializedMapper.selectById(id);
    }

    @Override
    public PageResult<DictSpecializedDO> getDictSpecializedPage(DictSpecializedPageReqVO pageReqVO) {
        return dictSpecializedMapper.selectPage(pageReqVO);
    }

}